﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ElectricityController : MonoBehaviour
{
    //private ElectricityController controller;
    [Header("Transform")]
    public Transform electricity;

    public Transform player1, player2;
    public SpriteRenderer[] lightnings;

    [Header("Property")]
    public float maxDistance = 3.0f;

    private float distance;
    private Vector3 direction;

    public void Refrash()
    {
	    Update();
    }

    private void Awake()
    {
        //controller = gameObject.GetComponent<ElectricityController>();
    }

    // Start is called before the first frame update
    private void Start()
    {

    }

    // Update is called once per frame
    private void Update()
    {
        distance = Mathf.Sqrt(Mathf.Pow(player1.position.x - player2.position.x, 2) + Mathf.Pow(player1.position.y - player2.position.y, 2));
        direction = Vector3.Normalize(player2.position - player1.position);

        electricity.right = direction;
        for (int i = 0; i < lightnings.Length; i++)
        {
            //lightnings[i].size.Set(distance, );
            lightnings[i].gameObject.transform.position = (player1.position + player2.position) / 2;
            lightnings[i].size = new Vector2(distance, lightnings[i].size.y);
        }
        //Debug.Log(lightnings[0].size.x);
    }

    private void Disappear()
    {
        for (int i = 0; i < lightnings.Length; i++)
        {
            lightnings[i].gameObject.SetActive(false);
        }
    }

    private void Appear()
    {
        for (int i = 0; i < lightnings.Length; i++)
        {
            lightnings[i].gameObject.SetActive(true);
        }
    }
}